package com.zhangx.study.demo.mybatis.processor;

import com.zhangx.study.demo.mybatis.bean.MyFactoryBean;
import com.zhangx.study.demo.mybatis.mapper.UserMapper;
import com.zhangx.study.demo.mybatis.service.A;
import com.zhangx.study.demo.mybatis.service.InterfaceC;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MyBeanFactoryPostProcessor
 * @Description TODO
 * @Author zhangx
 * @Date 2021/5/17 8:50
 **/
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    /**
     * 在bean工厂标准初始化之后修改应用上下文，所有的bd 会加载，但是还没有实例化
     * This allows for overriding or adding
     * properties even to eager-initializing beans.
     *
     * @param beanFactory the bean factory used by the application context
     * @throws BeansException in case of errors
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        GenericBeanDefinition beanDefinition = (GenericBeanDefinition)beanFactory.getBeanDefinition("user");

//        beanFactory.getBean("myFactoryBean");
//        System.out.println("获取 myFactoryBean");
//        在这里会报错，因为还没初始化
//        System.out.println(beanDefinition.getBeanClass());
//        如果没初始化，getBean会触发，对象会存在singletonObjects中
        beanFactory.getBean("user");
//        beanDefinition.setBeanClass(A.class);
//        放入接口也不会报错。。
        beanDefinition.setBeanClass(InterfaceC.class);
        System.out.println(beanDefinition.getBeanClass());
//      我们可以在后置处理器中注册bean，并实例化
        beanFactory.registerSingleton("xxx",new A());


    }



}
